package freakin.wify;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class SoapHelper {
	
	private final String TAG = "SoapHelper";
	
	private final String URL = "http://95.108.78.15/WebService.asmx";
	private final String NAMESPACE = "http://tempuri.org/";
	
	private final String METHOD_ADDAP = "AddAp";
	private final String ACTION_ADDAP = "http://tempuri.org/AddAp";
	
	private final String METHOD_CLOSEST_NET = "GetClosestOpenNetwork";
	private final String ACTION_CLOSEST_NET = "http://tempuri.org/GetClosestOpenNetwork";
	
	private final String METHOD_LOGIN = "LogIn";
	private final String ACTION_LOGIN = "http://tempuri.org/LogIn";
	
	private final String METHOD_REMOVE_NET = "RemoveNetwork";
	private final String ACTION_REMOVE_NET = "http://tempuri.org/RemoveNetwork";
	
    private SoapObject executeSoapCall(final SoapObject request,
            final String soapAction) throws IOException,
            XmlPullParserException {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.enc = SoapEnvelope.ENC;
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(URL);
        try {
        httpTransport.call(soapAction, envelope);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (SoapObject) envelope.bodyIn;
    }
    
    public void uploadAp(String bssid, String ssid, int freq, int lvl,
			String security, double lat, double lon) {
        
        SoapObject addApData = new SoapObject(NAMESPACE, METHOD_ADDAP);
        
        addApData.addProperty("bssid", bssid);
        addApData.addProperty("ssid", ssid);
        addApData.addProperty("channel", 1);
        addApData.addProperty("idCarrier", 1);
        addApData.addProperty("idEncryption", 1);
        addApData.addProperty("idMode", 1);
        addApData.addProperty("latitude", "" + lat);
        addApData.addProperty("longitude", "" + lon);
        //...
        try {
			executeSoapCall(addApData, ACTION_ADDAP);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
        
    }
    
    public Network getClosestOpenNetwork(double lat, double lon) {
    	
    	Network n = null;
    	
    	SoapObject addApData = new SoapObject(NAMESPACE, METHOD_CLOSEST_NET);
    	addApData.addProperty("lat", "" + lat);
        addApData.addProperty("lon", "" + lon);
        try {
        	n = new Network();
        	
        	SoapObject so = executeSoapCall(addApData, ACTION_CLOSEST_NET);
			SoapObject sss = (SoapObject)so.getNestedSoap(0);
			
			n.bssid = (String)sss.getProperty("bssid");
			n.latitude = (Double)sss.getProperty("lat");
			n.longitude = (Double)sss.getAttribute("lon");
			n.locationSet = true;
			Log.d(TAG, "returned network with bssid = " + n.bssid);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (RuntimeException e) {
			Log.e(TAG, "some properties haven't been found, I guess...");
		}
        
        return n;
    }
    
    public int logIn(String uname, String pwdSha256) {
    	SoapObject addApData = new SoapObject(NAMESPACE, METHOD_LOGIN);
    	addApData.addProperty("uname", "" + uname);
        addApData.addProperty("pwdSha256", "" + pwdSha256);
        
        SoapObject response = null;
        try {
			response = executeSoapCall(addApData, ACTION_LOGIN);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
        if(response != null) {
        	return (Integer)response.getProperty(0);
        } else {
        	return -1;
        }
    }
    
    public boolean removeNetwork(String bssid) {
    	SoapObject addApData = new SoapObject(NAMESPACE, METHOD_REMOVE_NET);
    	addApData.addProperty("bssid", "" + bssid);
        
        SoapObject response = null;
        try {
			response = executeSoapCall(addApData, ACTION_REMOVE_NET);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
        if(response != null) {
        	int rows_deleted =  (Integer)response.getProperty(0);
        	if(rows_deleted > 0)
        		return true;
        	else
        		return false;
        } else {
        	return false;
        }
    }

}
